﻿Public Class CompileOutput
    '' -o file
    ''     Place output in file file. This applies to whatever sort of output
    ''     is being produced, whether it be an executable file, an object file, 
    ''     an assembler file or preprocessed C code.
    Public OutputFile As String = ""

    Private _build As Boolean = True
    Public Property Build() As Boolean
        Get
            Return _build
        End Get
        Set(ByVal value As Boolean)
            If value = True Then
                _assembly = False
                _build = True
                _compile = False
                _preprocessing = False
            Else
                _assembly = False
                _build = False
                _compile = True
                _preprocessing = False
            End If
        End Set
    End Property

    '' -c
    ''     Compile or assemble the source files, but do not link. 
    ''     The linking stage simply is not done. The ultimate output 
    ''     is in the form of an object file for each source file.
    Private _compile As Boolean = False
    Public Property Compile() As Boolean
        Get
            Return _compile
        End Get
        Set(ByVal value As Boolean)
            If value = True Then
                _assembly = False
                _build = False
                _compile = True
                _preprocessing = False
            Else
                _assembly = False
                _build = True
                _compile = False
                _preprocessing = False
            End If
        End Set
    End Property

    '' -S
    ''     Stop after the stage of compilation proper; do not assemble. 
    ''     The output is in the form of an assembler code file for each 
    ''     non-assembler input file specified.
    Private _assembly As Boolean = False
    Public Property Assembly() As Boolean
        Get
            Return _assembly
        End Get
        Set(ByVal value As Boolean)
            If value = True Then
                _assembly = True
                _build = False
                _compile = False
                _preprocessing = False
            Else
                _assembly = False
                _build = True
                _compile = False
                _preprocessing = False
            End If
        End Set
    End Property

    '' -E
    ''     Stop after the preprocessing stage; do not run the compiler proper. 
    ''     The output is in the form of preprocessed source code, which is 
    ''     sent to the standard output.
    Private _preprocessing As Boolean = False
    Public Property Preprocessing() As Boolean
        Get
            Return _preprocessing
        End Get
        Set(ByVal value As Boolean)
            If value = True Then
                _assembly = False
                _build = False
                _compile = False
                _preprocessing = True
            Else
                _assembly = False
                _build = True
                _compile = False
                _preprocessing = False
            End If
        End Set
    End Property

    ''' <summary>
    ''' Convert an input file for compiling to output filename with appropriate
    ''' file's extension based on what type of output.
    ''' </summary>
    ''' <param name="inputFile">File to compile</param>
    ''' <returns>Output directory path and filename with appropriate extension</returns>
    Public Function ToOutputFile(ByVal inputFile As String) As String
        Dim fileInfo As New IO.FileInfo(inputFile)
        Dim base As String = fileInfo.DirectoryName & "\" & fileInfo.Name.Substring(0, fileInfo.Name.Length - fileInfo.Extension.Length)

        If Build Then
            Return base & ".exe"
        ElseIf Compile Then
            Return base & ".o"
        ElseIf Assembly Then
            Return base & ".s"
        Else
            Return base & ".i"
        End If
    End Function
End Class
